package com.java46.admin.dessertType.dao;


import com.java46.admin.base.exception.BusinessException;
import com.java46.admin.base.util.HibernateUtils;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.dessertType.entity.DesserttypeEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

public class DessertTypeDao {
        //查询
        public List<DesserttypeEntity> getAllDessertType(int offset,int pageSize,String condition) {
                List<DesserttypeEntity> list = new ArrayList<>();
                Session session = HibernateUtils.getSession();
                try {
                        Query query = session.createQuery("from com.java46.front.dessertType.entity.DesserttypeEntity where typeName like ?1");
                        query.setParameter(1, "%" + condition + "%");
                        query.setFirstResult(offset); //设置分页页码
                        query.setMaxResults(pageSize);
                        list = query.list();
                        return list;
                } catch (Exception e) {
                        //未抛异常
                } finally {
                        session.close();
                }
                return null;
        }

        //添加甜品类型
        public void addDessertType(DesserttypeEntity dessertType){
                Session session = HibernateUtils.getSession(); //拿到session
                session.save(dessertType);//添加
                session.beginTransaction().commit();//提交
                session.close();//关闭
        }

//        //删除甜品类型
//        public  void deleteDessertType(int dessertTypeId){
//                Session session = HibernateUtils.getSession(); //拿到session
//                Transaction transaction = session.beginTransaction();
//                Query query =session.createQuery(" delete from DesserttypeEntity where typeId=?1");
//                query.setParameter(1,dessertTypeId);
//                query.executeUpdate();
//                transaction.commit();
//                session.close();
//        }
        //通过ID查询甜品
        public  List<DessertEntity> queryDessertTypeId(int dessertTypeId){
                List<DessertEntity> list=new ArrayList<>();
                Session session = HibernateUtils.getSession();
                NativeQuery nativeQuery = HibernateUtils.getNativeQuery("SELECT * FROM dessert WHERE dessertType=?1");
                nativeQuery.setParameter(1, dessertTypeId);
                list = nativeQuery.list();
                session.close();
                return list;
        }

        //查询甜品类型总条数
        public int queryDessertTypeCount(String condition) {
                Session session = HibernateUtils.getSession();
                int count = 0;
                try {
                        NativeQuery nativeQuery = HibernateUtils.getNativeQuery("select COUNT(1) from dessertType where typeName like ?1");
                        nativeQuery.setParameter(1,"%" + condition + "%");
                        count = ((Number) nativeQuery.uniqueResult()).intValue();
                        return count;
                } catch (Exception e) {
                        throw new BusinessException("数据库异常！");
                } finally {
                        session.close();
                }

        }

    /**
     *通过商品类型Id查询商品类型
     * @return
     */
    public DesserttypeEntity query_DessertType_ById(int typeId){
        DesserttypeEntity desserttypeEntity = new DesserttypeEntity();
        Session session = HibernateUtils.getSession();
        try{
            Query query = HibernateUtils.getQuery("from com.java46.front.dessertType.entity.DesserttypeEntity dy where dy.typeId=?1");
            //给?赋值
            query.setParameter(1,typeId);
            if (query.list().size()!=0){
                desserttypeEntity =(DesserttypeEntity) query.list().get(0);
            }
        }catch (Exception e){
            throw new BusinessException("数据库异常...");
        }finally {
            session.close();
        }
        return desserttypeEntity;
    }
}
